package com.taotao.manager.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.taotao.common.bean.EasyUIResult;
import com.taotao.manager.service.ItemService;
import com.taotao.pojo.TbItem;

@RequestMapping("item")
@Controller
public class ItemController {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(ItemController.class);

	@Autowired
	private ItemService itemService;

	@RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<Void> saveItem(TbItem item, @RequestParam("desc") String desc,
            @RequestParam("itemParams") String itemParams) {
        try {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("新增商品，item = {}, desc = {}", item, desc);
            }
            Boolean bool = this.itemService.saveItem(item, desc, itemParams);
            if (bool) {
                // 新增成功 201
                if (LOGGER.isInfoEnabled()) {
                    LOGGER.info("新增商品成功! itemId = {}", item.getId());
                }
                return ResponseEntity.status(HttpStatus.CREATED).build();
            }
            if (LOGGER.isInfoEnabled()) {
                LOGGER.info("新增商品失败，但是没有异常抛出！ item = {}", item);
            }
        } catch (Exception e) {
            LOGGER.error("新增商品失败! item  = " + item, e);
        }
        // 500
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
	
	@RequestMapping(method=RequestMethod.GET)
	public ResponseEntity<EasyUIResult> queryItemList(
			@RequestParam(value="page",defaultValue="1") int page,
			@RequestParam(value="rows",defaultValue="10") int rows){
		try {
			EasyUIResult result = this.itemService.queryItemList(page,rows);
			return ResponseEntity.ok(result);
		} catch (Exception e) {
			LOGGER.error("查询商品列表出错！page = " +page +",rows = "+ rows, e);;
		}
		
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
		
	}
	
	
	@RequestMapping(method=RequestMethod.PUT)
	public ResponseEntity<Void> updateItem(TbItem item,@RequestParam("desc") String desc,
            @RequestParam("itemParams") String itemParams, @RequestParam("itemParamId") Long itemParamId) {
		try {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("编辑商品，item = {}, desc = {}", item, desc);
            }
            Boolean bool = this.itemService.updateItem(item, desc, itemParamId,itemParams);
            if (bool) {
                // 新增成功 201
                if (LOGGER.isInfoEnabled()) {
                    LOGGER.info("编辑商品成功! itemId = {}", item.getId());
                }
                return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
            }
            if (LOGGER.isInfoEnabled()) {
                LOGGER.info("编辑商品失败，但是没有异常抛出！ item = {}", item);
            }
        } catch (Exception e) {
            LOGGER.error("编辑商品失败! item  = " + item, e);
        }
        // 500
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
	}
}
